import { querySparesList,  actionSparesDel, actionSparesExport, actionSparesImport,saveAddSpares,saveEditSpares,
  querySparesInfo, queryRelatedEquipment, querySparesUseRecords, querySparesOperationlog,
} from '../services/spares';

export default {
  namespace: 'equipment_spares',

  state: {
    resdata: {
      list: [],
      pagination: {},
    },
    detaildata:{},
    log:{
      relatedEquipment:{},
      useRecords:{},
      sparesOperation:{},
    },
    loading: {
      reslist: true,
      model_submit:false,
      detailinfo:true,
    },
  },

  effects: {
    // 获取列表信息
    *fetch_spares_list({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { reslist: true },
      });
      const response = yield call(querySparesList, payload);
      response && (
        yield put({
          type: 'appendData',
          payload: response.data,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { reslist: false },
      });
    },

    // 批量删除操作
    *action_spares_del({ payload }, { call, put }) {
      const response = yield call(actionSparesDel, payload);
    },
    // 文件导出
    *action_spares_export({ payload }, { call, put }) {
      const response = yield call(actionSparesExport, payload);
    },
    // 文件导入
    *action_spares_import({ payload }, { call, put }) {
      const response = yield call(actionSparesImport, payload);
    },


    // 新增备件
    *add_spares({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { model_submit :true },
      });
      const response = yield call(saveAddSpares,payload);
      yield put({
        type: 'changeLoading',
        payload: { model_submit :false },
      });
    },

    // 备件修改
    *edit_spares({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { model_submit :true },
      });
      const response = yield call(saveEditSpares,payload);
      yield put({
        type: 'changeLoading',
        payload: { model_submit :false },
      });
    },


    // 获取备件详情
    *fetch_spares_info({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :true },
      });
      const response = yield call(querySparesInfo, payload);
      response && (
        yield put({
          type: 'appendDataDetail',
          payload:{
            ...response.data,
            attaPhoto:response.attaPhoto,
            attaDatum:response.attaDatum
          } ,
        })
      );
      yield put({
        type: 'changeLoading',
        payload: { detailinfo :false },
      });
    },


    // 获取单个备件关联设备
    *fetch_related_equipment({ payload }, { call, put }) {
      const response = yield call(queryRelatedEquipment, payload);
      response && (
        yield put({
          type: 'appendDataLog',
          payload:{relatedEquipment:response.data},
        })
      );
    },

    // 获取单个备件使用记录
    *fetch_use_record({ payload }, { call, put }) {
      const response = yield call(querySparesUseRecords, payload);
      response && (
        yield put({
          type: 'appendDataLog',
          payload:{useRecords:response.data},
        })
      );
    },

    // 获取单个备件操作日志
    *fetch_spares_operation({ payload }, { call, put }) {
      const response = yield call(querySparesOperationlog, payload);
      response && (
        yield put({
          type: 'appendDataLog',
          payload:{sparesOperation:response.data},
        })
      );

    },
  },

  reducers: {
    appendData(state, action) {
      return {
        ...state,
        resdata: action.payload,
      };
    },


    appendDataLog(state, action) {
      return {
        ...state,
        log: {
          ...state.log,
          ...action.payload,
        },
      };
    },

    changeLoading(state, action) {
      return {
        ...state,
        loading: {
          ...state.loading,
          ...action.payload,
        },
      };
    },
    appendDataDetail(state, action) {
      return {
        ...state,
        detaildata: action.payload,
      };
    },

    appendClearState(state, action) {
      return {
        ...state,
        resdata:{},
        detaildata:{},
        serviceEquipment:{},
        log:{}
      };
    },
  },
};
